package org.jeecg.modules.demo.ch.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.demo.ch.entity.ScrapManagement;
import org.jeecg.modules.demo.ch.service.IScrapManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;

/**
 * @Description: 报废品管理
 * @Author: jeecg-boot
 * @Date:   2022-12-16
 * @Version: V1.0
 */
@Api(tags="报废品管理")
@RestController
@RequestMapping("/ch/scrapManagement")
@Slf4j
public class ScrapManagementController extends JeecgController<ScrapManagement, IScrapManagementService> {
	@Autowired
	private IScrapManagementService scrapManagementService;

	/**
	 * 分页列表查询
	 *
	 * @param scrapManagement
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "报废品管理-分页列表查询")
	@ApiOperation(value="报废品管理-分页列表查询", notes="报废品管理-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<ScrapManagement>> queryPageList(ScrapManagement scrapManagement,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   String equipmentPurchaseDate_begin,
								   String equipmentPurchaseDate_end,
								   String entryTime_begin,
								   String entryTime_end,
								   String estimatedRetirementTime_begin,
								   String estimatedRetirementTime_end,
								   HttpServletRequest req) {
		log.info("传过来的参数:scrapManagement:{},equipmentPurchaseDate_begin:{},equipmentPurchaseDate_end:{},entryTime_begin:{},entryTime_end:{},estimatedRetirementTime_begin:{},estimatedRetirementTime_end:{}",scrapManagement,equipmentPurchaseDate_begin,equipmentPurchaseDate_end,entryTime_begin,entryTime_end,estimatedRetirementTime_begin,estimatedRetirementTime_end);
		String name = scrapManagement.getName();//名称
		String type = scrapManagement.getType();//类型
		String status = scrapManagement.getStatus();//状态
		String equipmentManager = scrapManagement.getEquipmentManager();//设备管理人
		String equipmentFixedAssetNo = scrapManagement.getEquipmentFixedAssetNo();//设备固定资产编号
		String itAssetNumber = scrapManagement.getItAssetNumber();//IT资产编号
		String remark = scrapManagement.getRemark();//备注
		String enteredBy = scrapManagement.getEnteredBy();//录入人
		//		QueryWrapper<ScrapManagement> queryWrapper = QueryGenerator.initQueryWrapper(scrapManagement, req.getParameterMap());
		LambdaQueryWrapper<ScrapManagement> scrapManagementLambdaQueryWrapper = new QueryWrapper<ScrapManagement>().lambda()
				.like(StringUtils.hasLength(name), ScrapManagement::getName, name)
				.like(StringUtils.hasLength(type), ScrapManagement::getType, type)
				.like(StringUtils.hasLength(status), ScrapManagement::getStatus, status)
				.like(StringUtils.hasLength(equipmentManager), ScrapManagement::getEquipmentManager, equipmentManager)
				.like(StringUtils.hasLength(equipmentFixedAssetNo), ScrapManagement::getEquipmentFixedAssetNo, equipmentFixedAssetNo)
				.like(StringUtils.hasLength(itAssetNumber), ScrapManagement::getItAssetNumber, itAssetNumber)
				.like(StringUtils.hasLength(remark), ScrapManagement::getRemark, remark)
				.like(StringUtils.hasLength(enteredBy), ScrapManagement::getEnteredBy, enteredBy)
				.ge(StringUtils.hasLength(equipmentPurchaseDate_begin), ScrapManagement::getEquipmentPurchaseDate, equipmentPurchaseDate_begin)
				.le(StringUtils.hasLength(equipmentPurchaseDate_end), ScrapManagement::getEquipmentPurchaseDate, equipmentPurchaseDate_end)
				.ge(StringUtils.hasLength(entryTime_begin), ScrapManagement::getEntryTime, entryTime_begin)
				.le(StringUtils.hasLength(entryTime_end), ScrapManagement::getEntryTime, entryTime_end)
				.ge(StringUtils.hasLength(estimatedRetirementTime_begin), ScrapManagement::getEstimatedRetirementTime, estimatedRetirementTime_begin)
				.le(StringUtils.hasLength(estimatedRetirementTime_end), ScrapManagement::getEstimatedRetirementTime, estimatedRetirementTime_end);
		Page<ScrapManagement> page = new Page<ScrapManagement>(pageNo, pageSize);
		IPage<ScrapManagement> pageList = scrapManagementService.page(page, scrapManagementLambdaQueryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param scrapManagement
	 * @return
	 */
	@AutoLog(value = "报废品管理-添加")
	@ApiOperation(value="报废品管理-添加", notes="报废品管理-添加")
	//@RequiresPermissions("org.jeecg.modules.demo:scrap_management:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody ScrapManagement scrapManagement) {
		//设置录入时间
		scrapManagement.setEntryTime(new Date());
		scrapManagementService.save(scrapManagement);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param scrapManagement
	 * @return
	 */
	@AutoLog(value = "报废品管理-编辑")
	@ApiOperation(value="报废品管理-编辑", notes="报废品管理-编辑")
	//@RequiresPermissions("org.jeecg.modules.demo:scrap_management:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody ScrapManagement scrapManagement) {
		scrapManagementService.updateById(scrapManagement);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "报废品管理-通过id删除")
	@ApiOperation(value="报废品管理-通过id删除", notes="报废品管理-通过id删除")
	//@RequiresPermissions("org.jeecg.modules.demo:scrap_management:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		scrapManagementService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "报废品管理-批量删除")
	@ApiOperation(value="报废品管理-批量删除", notes="报废品管理-批量删除")
	//@RequiresPermissions("org.jeecg.modules.demo:scrap_management:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.scrapManagementService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "报废品管理-通过id查询")
	@ApiOperation(value="报废品管理-通过id查询", notes="报废品管理-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<ScrapManagement> queryById(@RequestParam(name="id",required=true) String id) {
		ScrapManagement scrapManagement = scrapManagementService.getById(id);
		if(scrapManagement==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(scrapManagement);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param scrapManagement
    */
    //@RequiresPermissions("org.jeecg.modules.demo:scrap_management:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ScrapManagement scrapManagement) {
        return super.exportXls(request, scrapManagement, ScrapManagement.class, "报废品管理");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("scrap_management:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ScrapManagement.class);
    }

}
